package org.ims.inv.web.action.project;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ims.inv.model.flow.ModelFlowDef;
import org.ims.inv.model.project.ModelProject;
import org.ims.inv.model.system.ModelAppUser;
import org.ims.inv.web.util.ContextUtil;

import cn.trymore.core.util.UtilString;
import cn.trymore.core.web.paging.PaginationSupport;
import cn.trymore.core.web.paging.PagingBean;

/**
 * Action: 项目库 (我的项目库, 公司项目库)
 * 
 * @author Jeccy.Zhao
 *
 */
public class ProjectArchiveAction
extends ProjectBaseAction
{
	/**
	 * The LOGGER
	 */
	private static final Logger LOGGER = Logger.getLogger(ProjectArchiveAction.class);
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * Archive Index Page
	 */
	public ActionForward pageProjectArchiveIndex (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		
		try
		{
			// 设置条件查询表单
			ModelProject formEntity = (ModelProject) form;
			String projectManagerName = request.getParameter("mgr.username");
			if (UtilString.isNotEmpty(projectManagerName))
			{
				ModelAppUser manager = new ModelAppUser();
				manager.setUsername(projectManagerName);
				formEntity.setProjectManager(manager);
			}
			request.setAttribute("formEntity", formEntity);
			
			// 加载流程环节模板
			ModelFlowDef activatedDef = 
				this.serviceWorkFlow.getServiceFlowDefinition().getCurrentAppliedDefinition();
			if (activatedDef != null)
			{
				request.setAttribute("processes", activatedDef.getTemplates());
			}
			
			// 获取项目所属行业
			request.setAttribute("dictInds", this.getDictionariesByType("DICT_PRO_INDUSTRY"));
			
			this.outWriteAllParameters(request);
			
			return mapping.findForward("page.project.archive.index");
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	/**
	 * <b>[WebAction]</b> 
	 * <br/>
	 * 根据状态加载项目库
	 */
	public ActionForward actionLoadProjectArchivesByStatus (ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
	{
		try
		{
			String status = request.getParameter("status");
			if (UtilString.isNotEmpty(status))
			{
				ModelProject formEntity = (ModelProject) form;
				
				// 项目名称编码转换 (避免GET请求出现乱码)
				if (UtilString.isNotEmpty(formEntity.getProjectName()))
				{
					formEntity.setProjectName(new String(
						formEntity.getProjectName().getBytes("ISO-8859-1"), "UTF-8"));
				}
				
				// 项目负责人
				String projectManagerName = request.getParameter("mgr.username");
				if (UtilString.isNotEmpty(projectManagerName))
				{
					ModelAppUser manager = new ModelAppUser();
					manager.setUsername(
							new String(projectManagerName.getBytes("ISO-8859-1"), "UTF-8"));
					formEntity.setProjectManager(manager);
				}
				
				// 行业类别
				String projectTypeId = request.getParameter("projectTypeId");
				if (UtilString.isNotEmpty(projectTypeId))
				{
					formEntity.setProjectType(this.serviceAppDictionary.get(projectTypeId));
				}
				
				// 设置可见范围
				Integer visible = null;
				String cat = request.getParameter("cat");
				if (UtilString.isNotEmpty(cat) && "public".equalsIgnoreCase(cat))
				{
					visible = ModelProject.EProjectVisible.PUBLIC.getValue();
					
					ModelAppUser currentUser = ContextUtil.getCurrentUser();
					if (currentUser.isSuperUser() || currentUser.isLeaderShip() || currentUser.isAssistant())
					{
						visible = ModelProject.EProjectVisible.LEADERSHIP.getValue();
					}
				}
				
				// 获取分页数据
				PagingBean pagingBean = this.getPagingBean(request);
				PaginationSupport<ModelProject> pagingData = 
					this.serviceProject.getProjectsByArchiveStatus(formEntity, status, visible, pagingBean);
				
				request.setAttribute("datagrid", pagingData);
				request.setAttribute("formEntity", formEntity);
				
				this.outWritePagination(request, pagingBean, pagingData);
				
				this.outWriteAllParameters(request);
				return mapping.findForward("base.project.datagrid");
			}
			else
			{
				return ajaxPrint(response, getErrorCallback("未知的请求参数."));
			}
		}
		catch (Exception e)
		{
			LOGGER.error("Exception raised when loading page", e);
			return ajaxPrint(response, getErrorCallback("数据加载失败,原因:" + e.getMessage()));
		}
	}
	
	public static Logger getLogger()
	{
		return LOGGER;
	}
	
}
